{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mxnet import nd, gpu, autograd\n",
    "from mxnet.gluon import nn, data, loss, trainer, utils\n",
    "from mxnet.gluon.data.vision import datasets, transforms\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[1. 1. 1. 1.]\n",
       " [1. 1. 1. 1.]\n",
       " [1. 1. 1. 1.]]\n",
       "<NDArray 3x4 @gpu(0)>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = nd.ones((3, 4), ctx=gpu())\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[1. 1. 1. 1.]\n",
       " [1. 1. 1. 1.]\n",
       " [1. 1. 1. 1.]]\n",
       "<NDArray 3x4 @gpu(1)>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.copyto(gpu(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[1.6686509 1.1740919 1.3850025 1.2467831]\n",
       " [1.3513434 1.8404298 1.6369917 1.12847  ]\n",
       " [1.1724988 1.9368206 1.5918345 1.9497097]]\n",
       "<NDArray 3x4 @gpu(0)>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = nd.random.uniform(shape=(3, 4), ctx=gpu())\n",
    "x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = nn.Sequential()\n",
    "net.add(\n",
    "    nn.Conv2D(channels=6, kernel_size=5, activation='relu'),\n",
    "    nn.MaxPool2D(pool_size=2, strides=2),\n",
    "    nn.Conv2D(channels=16, kernel_size=3, activation='relu'),\n",
    "    nn.MaxPool2D(pool_size=2, strides=2),\n",
    "    nn.Flatten(),\n",
    "    nn.Dense(120, activation=\"relu\"),\n",
    "    nn.Dense(84, activation=\"relu\"),\n",
    "    nn.Dense(10)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.load_parameters('net.params', ctx=gpu())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[ 0.6284458   0.21993409  1.1261961   0.06318956 -0.4687658  -1.3573954\n",
       "   1.1736336  -1.2489953   1.4780961  -1.2589195 ]]\n",
       "<NDArray 1x10 @gpu(0)>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = nd.random.uniform(shape=(1, 1, 28, 28), ctx=gpu())\n",
    "net(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 256\n",
    "transformer = transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize(0.13, 0.31)\n",
    "])\n",
    "train_data = data.DataLoader(datasets.FashionMNIST(train=True).transform_first(transformer), batch_size, shuffle=True, num_workers=4)\n",
    "valid_data = data.DataLoader(datasets.FashionMNIST(train=False).transform_first(transformer), batch_size, shuffle=True, num_workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1: loss 1.863, in 3.7 sec\n",
      "Epoch 2: loss 0.724, in 3.1 sec\n",
      "Epoch 3: loss 0.560, in 3.1 sec\n",
      "Epoch 4: loss 0.484, in 3.1 sec\n",
      "Epoch 5: loss 0.444, in 3.1 sec\n",
      "Epoch 6: loss 0.408, in 3.0 sec\n",
      "Epoch 7: loss 0.383, in 3.1 sec\n",
      "Epoch 8: loss 0.358, in 3.5 sec\n",
      "Epoch 9: loss 0.343, in 3.1 sec\n",
      "Epoch 10: loss 0.329, in 2.9 sec\n"
     ]
    }
   ],
   "source": [
    "devices = [gpu(0), gpu(1)]\n",
    "net.collect_params().initialize(force_reinit=True, ctx=devices)\n",
    "\n",
    "softmax_cross_entropy = loss.SoftmaxCrossEntropyLoss()\n",
    "optimizer = trainer.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1})\n",
    "\n",
    "for epoch in range(1, 11):\n",
    "    train_loss = 0.\n",
    "    tic = time.time()\n",
    "    for Xs, ys in train_data:\n",
    "        data_list = utils.split_and_load(Xs, devices)\n",
    "        label_list = utils.split_and_load(ys, devices)\n",
    "        with autograd.record():\n",
    "            losses = [softmax_cross_entropy(net(X), y) for X, y in zip(data_list, label_list)]\n",
    "        for l in losses:\n",
    "            l.backward()\n",
    "        optimizer.step(batch_size)\n",
    "        train_loss += sum([l.sum().asscalar() for l in losses])\n",
    "    print(\"Epoch %d: loss %.3f, in %.1f sec\" % (epoch, train_loss / len(train_data) / batch_size, time.time() - tic))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "455583263cdbd810a2b2dc0736388df1171afe9106774a1560537fda80d7bd38"
  },
  "kernelspec": {
   "display_name": "Python 3.6.13 64-bit ('mxnet': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
